package cn.bdqn.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import cn.bdqn.define.Cost;
import cn.bdqn.entity.User;
import cn.bdqn.service.user.UserService;

@Controller
@RequestMapping("/user")
public class UserController {
	
	@Resource
	private UserService userService;
	
	//查看用户详情
	@RequestMapping("/view/{id}/{userCode}")
	public String view(@PathVariable("id") String id,@PathVariable("userCode") String userCode, Model model) {
		User user = userService.getUserById(id);
		model.addAttribute("user", user);
		return "userview";
	}
	
	//跳转到用户修改页面
	@RequestMapping("/toUpdateUser")
	public String toUpdateUser(String userId,Model model) {
		User user = userService.getUserById(userId);
		model.addAttribute("user", user);
		return "usermodify";
	}
	
	//保存修改
	@RequestMapping("/updateUser")
	public String updateUser(User user) {
		boolean state = userService.modify(user);
		if(!state) {
			return "usermodify";
		}
		return "redirect:/user/list";
	}
	
	//跳转到用户新增页面
	@RequestMapping("/addUser")
	public String addUser(@ModelAttribute User user) {
		return "useradd";
	}
	
	//新增用户保存
	@RequestMapping("/addUserSave")
	public String addUserSave(User user,MultipartFile file1,MultipartFile file2) {
		
			String fileName = "";
			//1.文件是否存在
			if(!file1.isEmpty()) {
				//2.文件存放位置
				fileName = file1.getOriginalFilename();
				String filePath = "d:"+File.separator+"file"+File.separator+file1.getOriginalFilename();
				//3.移动文件到指定位置
				try {
					file1.transferTo(new File(filePath));
					user.setIdPicPath(fileName);
				} catch (IllegalStateException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
			//2.文件是否存在
			if(!file2.isEmpty()) {
				//2.文件存放位置
				fileName = file2.getOriginalFilename();
				String filePath = "d:"+File.separator+"file"+File.separator+file2.getOriginalFilename();
				//3.移动文件到指定位置
				try {
					file2.transferTo(new File(filePath));
					user.setIdPicPath(fileName);
				} catch (IllegalStateException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		
		
		boolean state = userService.add(user);
		if(!state) {
			return "useradd";
		}
		return "redirect:/user/list";
	}
	
	
	//跳转到登陆页面
	@RequestMapping("/toLogin")
	public String toLogin() {
		return "login";
	}
	
	//登陆方法
	@RequestMapping("/doLogin")
	public String doLogin(String userCode,String userPassword,HttpSession session,HttpServletRequest request) {
		User user = userService.login(userCode, userPassword);
		if(user==null) {
//			request.setAttribute(Cost.Message.LOGINERROR, "用户名或者密码错误");
			throw new RuntimeException("用户名或者密码错误");
			//return "login";
		}
		session.setAttribute(Cost.User.USERSESSION, user);
		return "redirect:/user/toFrame";
	}
	
	//toFrame
	@RequestMapping("/toFrame")
	public String toFrame() {
		return "frame";
	}
	
	//list
	@RequestMapping("/list")
	public String list(User user,Integer pageIndex,Integer pageSize,Model model) {
		//处理分页参数
		if(pageIndex==null) {
			pageIndex = 1;
		}
		
		if(pageSize==null) {
			pageSize = 5;
		}
		
		//获取userlist
		List<User> userList = userService.getUserList(user.getUserName(), user.getUserRole(), pageIndex, pageSize);
		int count = userService.getUserCount(user.getUserName(), user.getUserRole());
		model.addAttribute("userList", userList);
		model.addAttribute("totalCount", count);
		int totalPageCount = count%pageSize==0?count/pageSize:count/pageSize+1;
		model.addAttribute("totalPageCount", totalPageCount);
		model.addAttribute("currentPageNo", pageIndex);
		return "userlist";
	}
	
	@ExceptionHandler(RuntimeException.class)
	public String exceptionHandler(HttpServletRequest request,RuntimeException e) {
		request.setAttribute(Cost.Message.LOGINERROR, e.getMessage());
		return "error";
	}
	
}
